﻿using System.Collections.Generic;
using Entity;
using UnityEngine;
using Utils;

namespace Mapper
{
    class CardMapper
    {
        private const string TABLE_NAME = "card";

        public CardMapper()
        {
            MySQLUtil.CreateConnection();
        }

        public List<Card> ListAllCards()
        {
            return MySQLUtil.SelectData<Card>(TABLE_NAME, "*", null);
        }

        public Card GetCardById(long cardId)
        {
            return MySQLUtil.SelectData<Card>(TABLE_NAME, "*", "cardId = " + cardId)[0];
        }

        public List<CollectCardVO> ListCollectCardVOByUserId(long userId)
        {
            return MySQLUtil.SelectData<CollectCardVO>(
                $"select card.* , card_user_rel.cardCount from card , card_user_rel where card_user_rel.cardId = card.cardId and card_user_rel.userId = {userId}");
        }

        public List<CollectCardVO> ListCollectCardVOPageByUserId(long userId, int current, int pageSize)
        {
            int start = (current - 1) * pageSize;
            int end = current * pageSize;
            return MySQLUtil.SelectData<CollectCardVO>(
                $"select card.* , card_user_rel.cardCount from card , card_user_rel where card_user_rel.cardId = card.cardId and card_user_rel.userId = {userId} limit {start} , {end}");
        }

        public List<Card> ListCardByDeckId(long deckId)
        {
            return MySQLUtil.SelectData<Card>(
                $"select card.* from card , deck , deck_card_rel rel where rel.deckId = {deckId} and rel.deckId = deck.deckId and rel.cardId = card.cardId");
        }

        public List<CollectCardVO> ListCollectCardVOByDeckId(long deckId)
        {
            return MySQLUtil.SelectData<CollectCardVO>(
                $"select card.* , rel.cardCount from card , deck , deck_card_rel rel where rel.deckId = {deckId} and rel.deckId = deck.deckId and rel.cardId = card.cardId");
        }
        
        public List<CollectCardVO> ListCollectCardVOByUserIdAndGtCardCount(long userId , int cardCount)
        {
            return MySQLUtil.SelectData<CollectCardVO>(
                $"select card.* , card_user_rel.cardCount from card , card_user_rel where card_user_rel.cardId = card.cardId and card_user_rel.userId = {userId} and card_user_rel.cardCount > {cardCount}");
        }

        public void AddCardToUser(long cardId, long userId, int count)
        {
            string sql = $"insert into card_user_rel (cardId ,userId ,cardCount) values ({cardId},{userId},{count})";
            Debug.Log("执行的SQL语句：" + sql);
            MySQLUtil.AddData(sql);
        }

        public void UpdateUserCardCount(long cardId, long userId, int count)
        {
            string sql = $"update card_user_rel set cardCount = {count} where userId = {userId} and cardId = {cardId}";
            Debug.Log("执行的SQL语句：" + sql);
            MySQLUtil.UpdateData(sql);
        }

        public CollectCardVO GetCollectCardVOByCardIdAndUserId(long cardId, long userId)
        {
            string sql =
                $"select card.* , rel.cardCount from card , card_user_rel rel where card.cardId = rel.cardId and rel.cardId = {cardId} and rel.userId = {userId}";
            List<CollectCardVO> result = MySQLUtil.SelectData<CollectCardVO>(sql);
            if (result != null && result.Count > 0)
            {
                return result[0];
            }
            return null;
        }

        public long AddCard(Card card)
        {
            return MySQLUtil.AddData<Card>(card, TABLE_NAME);
        }

        public void CloseConnection()
        {
            MySQLUtil.Close();
        }
    }
}